package com.banciyuan.bcywebview.biz.ask.question;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.base.helper.FlagUtils;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import org.json.JSONObject;

public class QuestionHelper {

    private Context mContext;
    private RequestQueue mQueue;

    public QuestionHelper(Context mContext) {
        this.mContext = mContext;
        mQueue = VolleyQueue.getRquest(mContext);
    }

    public Context getContext() {
        return mContext;
    }

    public void getFrontData(String token, int pagenum, final GetQuestionData getQuestionData) {

        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                        getQuestionData.succ(jsonObject.getString(HttpUtils.UPLOAD_DATA));
                    } else {
                        getQuestionData.fail(jsonObject.getString(HttpUtils.UPLOAD_DATA));
                    }
                } catch (Exception e) {
                    getQuestionData.fail("fail");
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                getQuestionData.fail("fail");
            }
        };
        CommonApiUtils.getAnswer(mContext, mQueue, onSuccess, onError, pagenum, token);
    }

    public void delQuestion(String ua_id) {
        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        };
        CommonApiUtils.delAsk(mContext, mQueue, onSuccess, onError, ua_id,
                UserDataHelper.getInstance(mContext).getToken());
    }

    public interface GetQuestionData {
        void succ(String json);

        void fail(String fail);
    }
}